//
//  UpUserIconControllerViewController.m
//  K-Music
//
//  Created by apple on 16/1/17.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "UpUserIconControllerViewController.h"
@interface UpUserIconControllerViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UIImageView * tapImageView;

@property (nonatomic,assign) BOOL * image_NO;

@property (nonatomic,assign) BOOL * back_NO;




@end

@implementation UpUserIconControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithName:@"确定" Color:[UIColor blackColor] target:self action:@selector(determine)];
    
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"上传图片";
    
    [self creatUI];
}

- (void)back_back
{
    if ( _back_NO == false ) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        
    }
}

- (void)creatUI
{
    self.tapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)];
    self.tapImageView.userInteractionEnabled = YES;
    self.tapImageView.image = [UIImage imageNamed:@"Choose_pic"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(upDateIcon:)];
    [self.tapImageView addGestureRecognizer:tap];
    [self.view addSubview:self.tapImageView];
}

-(void)upDateIcon:(UITapGestureRecognizer *)sender{
    self.tapImageView =(UIImageView*)sender.view ;
    
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"照相机",@"本地相簿",nil];
    [actionSheet showInView:self.view];
}

#pragma mark                 --------UIActionSheet Delegate--------
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0://照相机
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
            break;
        case 1://本地相簿
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}
#pragma mark           ----------UIImagePickerController Delegate---------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeImage]) {
        UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
        [self performSelector:@selector(saveImage:)  withObject:img afterDelay:0.5];
        
        NSData *data =  UIImageJPEGRepresentation(img, 0.2);
        [Seven_httpKeyData requestSeven_token:data sucess:^(id responseObjectUrl) {
         
            NSLog(@"上传7 牛成功 url －－>:%@",responseObjectUrl);
            
        } failure:^(id Url) {
            
            
        }];
        
        }
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)ChangeUser:(NSString*)responseObjectUrl
{
    [MBProgressHUD showErrorWithStatus:@"头像正在上传..." toView:self.view];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    manager.requestSerializer =[AFJSONRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/users"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * aid =[NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
    
    
    NSDictionary * submit = @{@"id":aid,@"avatar":responseObjectUrl};
    
    [manager POST:intactURL parameters:submit success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary * result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         
         NSLog(@"================:%@",result);
         
         _image_NO = true;
         
         [MBProgressHUD showErrorWithStatus:@"头像上传成功" toView:self.view];
         
     }failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"%@", error);
     }];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)saveImage:(UIImage *)image
{
    [self.tapImageView setImage:image];
}

-(void)determine{
    
    NSLog(@"确定");

    if ( _image_NO == false) {
        
        MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请选择图片";
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:2];
        
        
    }else
    {
        if (self.changeUserIconBlook != nil)
        {
            self.changeUserIconBlook(self.tapImageView.image);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
        _image_NO = false;
        
    }
    
}

-(void)changeUserIconMethods:(changeUserIconBlook)blook{
    
    self.changeUserIconBlook = blook;
    
}
@end
